package cn.mogugo.util.tools;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;
import java.util.TimeZone;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

public class DateTools {
	
	public static Logger log = Logger.getLogger(DateTools.class);
	
	
	
	public static final String FULL_YMDHMS = "yyyy-MM-dd HH:mm:ss";	
	public static final String FULL_YMDHM = "yyyy-MM-dd HH:mm";
	public static final String FULL_YMDH = "yyyy-MM-dd HH";
	public static final String FULL_YMDH1 = "yyyy-MM-dd HH时";
	public static final String FULL_YMD = "yyyy-MM-dd";
	public static final String FULL_YMD1 = "yyyyMMdd";
	public static final String FULL_YMD2 = "yyyy年MM月dd日";
	public static final String FULL_YMD3 = "yy/MM/dd";

	public static final String FULL_YM = "yyyy-MM";
	public static final String FULL_YM1 = "yyyyMM";
	
	public static final String FULL_HMS = "HH:mm:ss";
	
	
	public static String dateToString(Date date) {
		return 	dateToString(date , FULL_YMD);
	}
	
	public static String dateToString(Date date, String pattern) {
		if (date == null) {
			return "";
		}
		SimpleDateFormat dateFormat = new SimpleDateFormat(pattern);
		return dateFormat.format(date);		
	}
	
	
	public static Date stringToDate(String strDate){
		return stringToDate(strDate , DateTools.FULL_YMD);

	}
	
	
	public static Integer getMonth(Date adate){
		Calendar c = Calendar.getInstance();
		c.setTime(adate);
		return c.get(Calendar.MONTH)+1;
	}
	
	public static Integer getCurYear(String strDate){
		Date adate = stringToDate(strDate);
		return adate.getYear() + 1900;
	}

	
	public static Integer getCurYear(){
		Date adate = new Date();
		return adate.getYear() + 1900;
	}
	
	/**
	 * 获取星期几
	 * @return
	 */
	public static Integer getCurDayOfWeek(){
		return getDayOfWeek(getCurDate());
	}
	
	public static Integer getDayOfWeek(Date adate){
		Calendar c = Calendar.getInstance();
		c.setTime(adate);
		Integer dayOfWeek = c.get(Calendar.DAY_OF_WEEK);
		if(dayOfWeek == 1){
			return 7;
		}else{
			return dayOfWeek -1;
		}
	}
	
	
	
	public static Date stringToDate(String strDate,String pattern){
		
		if(StringUtils.isEmpty(strDate)){
			return null;
		}else{
			SimpleDateFormat df = null;
			Date date = null;
			df = new SimpleDateFormat(pattern , java.util.Locale.CHINESE);
			try{
				date = df.parse(strDate);
			}catch(ParseException e){
				log.error(e);
			}
			return date;
		}

	}
	
	/**
	 * 获取当前日期时间
	 * @return
	 */
	public static Date getCurDateTime(){
		return new Date();
	}
	/**
	 * 获取当前日期 yyyy-MM-dd
	 * @return
	 */
	public static Date getCurDate(){
		Date adate = new Date();
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		String strDate = df.format(adate);
		Date rtnDate = null;
		try{
			rtnDate = df.parse(strDate);
		}catch(Exception e){
			rtnDate = adate;
		}
		return rtnDate;
	}
	
	
	public static Date getCurDate(String patten){
		Date adate = new Date();
		DateFormat df = new SimpleDateFormat(patten);
		String strDate = df.format(adate);
		Date rtnDate = null;
		try{
			rtnDate = df.parse(strDate);
		}catch(Exception e){
			rtnDate = adate;
		}
		return rtnDate;
	}
	
	public static String getCurDateString(String patten){
		Date adate = new Date();
		DateFormat df = new SimpleDateFormat(patten);
		String strDate = df.format(adate);
		return strDate;
	}
	
	public static String getCurDateString(){
		return DateTools.dateToString(getCurDate());
	}
	
	
	public static String getCurDateTimeString(){
		return DateTools.dateToString(getCurDateTime() , FULL_YMDHMS);
	}
	
	
	/**
     * 计算两个日期之间相差的天数
     * 
     * @param date1
     * @param date2
     * @return
     */
    public static int diffdates(Date date1, Date date2) {
        int result = 0;

        GregorianCalendar gc1 = new GregorianCalendar();
        GregorianCalendar gc2 = new GregorianCalendar();

        gc1.setTime(date1);
        gc2.setTime(date2);
        
    	gc1.clear(Calendar.MILLISECOND);
    	gc1.clear(Calendar.SECOND);
    	gc1.clear(Calendar.MINUTE);
    	gc1.clear(Calendar.HOUR_OF_DAY);

    	gc2.clear(Calendar.MILLISECOND);
    	gc2.clear(Calendar.SECOND);
    	gc2.clear(Calendar.MINUTE);
    	gc2.clear(Calendar.HOUR_OF_DAY);

    	if(gc1.before(gc2)){
        	while (gc1.before(gc2)) {
    	    	gc1.add(Calendar.DATE, 1);
    	    	result++;
        	}
    	}else{
        	while (gc2.before(gc1)) {
    	    	gc1.add(Calendar.DATE, -1);
    	    	result -- ;
        	}
    	}
    	return result;
    }
    
    
	/**
	 * 根据传入的日期取得参数对应月份的最大日期
	 * @param adate
	 * @return
	 */
	public static Date rtnMaxDateInMonth(Date adate){
		Calendar cal = Calendar.getInstance();
		cal.setTime(adate);
		cal.set(cal.DAY_OF_MONTH, 1);
		cal.roll(cal.DATE, false);
		return cal.getTime();
	}
	/**
	 * 根据传入的日期取得参数对应月份的最小日期
	 * @param adate
	 * @return
	 */
	public static Date rtnMinDateInMonth(Date adate){
		Calendar cal = Calendar.getInstance();
		cal.setTime(adate);
		cal.set(cal.DATE, 1);
		return cal.getTime();
	}
	
	public static Long getCurTime(){
		Date curDate = new Date();
		return curDate.getTime();
	}
	
	/**
	 * 取得当前日期的头一天的日期
	 * @return
	 */
	public static Date getYestoday(){
		Date curDate = new Date();
		Calendar cal = Calendar.getInstance();
		cal.setTime(curDate);
		cal.add(cal.DATE, -1);
		return cal.getTime();
	}

	/**
	 * 根据参数日期返回上月的日期，只保证结果为curDate的上月，日期不能保证
	 * @param curDate
	 * @return
	 */
	public static Date rtnLastDate(Date curDate){
		Calendar cal = Calendar.getInstance();
		cal.setTime(curDate);
		cal.add(cal.MONTH, -1);
		return cal.getTime();
	}

	public static Date formatLongToDate(long alongdate){
		Calendar c =  Calendar.getInstance();
		c.setTimeInMillis(alongdate);
		return c.getTime();
	}
	
	
	public static String formatLongToStrDate(long alongdate){
		Calendar c =  Calendar.getInstance();
		c.setTimeInMillis(alongdate);
		return dateToString(c.getTime() , FULL_YMDHMS);
	}
	
	public static Date getDateAfterHour(Date adate , Integer ahour){
		if(adate == null){
			adate = new Date();
		}
		Calendar c =  Calendar.getInstance();
		c.setTime(adate);
		c.add(Calendar.HOUR, ahour);
		Date anothorDate = c.getTime();
		return anothorDate;
		
	}
	
	
	public static Date getDateAfterMin(Date adate , int amin){
		if(adate == null){
			adate = new Date();
		}
		Calendar c =  Calendar.getInstance();
		c.setTime(adate);
		c.add(Calendar.MINUTE, amin);
		Date anothorDate = c.getTime();
		return anothorDate;
		
	}
	
	/**
	 * 获取基于adate日期的day天之前的日期
	 * @param adate
	 * @param day
	 * @return
	 */
	public static Date getDateAfterDay(Date adate , Integer day){
		if(adate == null){
			adate = new Date();
		}
		Calendar c =  Calendar.getInstance();
		c.setTime(adate);
		c.add(Calendar.DAY_OF_YEAR, day);
		Date anothorDate = c.getTime();
		return anothorDate;
	}

	
	/**
	 * 获取当前日期的上一个季度
	 * @return
	 */
	public static String getLastQuarter(){
		String strQuarter = "";
		Calendar c = Calendar.getInstance();
		int year = c.get(Calendar.YEAR);
		int month = c.get(Calendar.MONTH);
		if(month >= 0 && month <=2){
			strQuarter = (year-1) + "-4"; 
		}else{
			strQuarter = year + "-" + (month/3 -1);
		}
		return strQuarter;
	}
	
	
	/**
	 * 获取当前日期的上一个季度
	 * @return
	 */
	public static String getQuarterString(Date adate){
		String strQuarter = "";
		Calendar c = Calendar.getInstance();
		c.setTime(adate);
		int year = c.get(Calendar.YEAR);
		int month = c.get(Calendar.MONTH);
		
		strQuarter = year + "年" + (month/3+1) + "季";
		
		return strQuarter;
	}
	
	/**
	 * 获取当前日期的所在的季度
	 * @return
	 */
	public static Date[] getCurQuarterBeginAndEndDate(Date curDate){
		Date[] adate = new Date[2];
		String strQuarter = "";
		Calendar c = Calendar.getInstance();
		c.setTime(curDate);
		int year = c.get(Calendar.YEAR);
		int month = c.get(Calendar.MONTH);
		adate[0] = DateTools.stringToDate(year + "-" + ((month/3)*3+1) + "-01");
		
		Date endDate = DateTools.stringToDate(year + "-" + ((month/3)*3+4) + "-01");
		Calendar c1 = Calendar.getInstance();
		c1.setTime(endDate);
		c1.add(Calendar.DAY_OF_MONTH, -1);
		adate[1] = c1.getTime();
		
		return adate;
	}
	
	public static String[] getCurQuarterBeginAndEndDateForString(Date curDate){
		Date[] adate = getCurQuarterBeginAndEndDate(curDate);
		String[] strdate = new String[2];
		strdate[0] = DateTools.dateToString(adate[0]);
		strdate[1] = DateTools.dateToString(adate[1]);
		return strdate;
	}
	
	
	
	public static Date[] getMondayAndFridy(Date curDate){
		Date[] returnDate = new Date[2];
		Calendar c = Calendar.getInstance();
	    c.setTime(curDate); 
	    int dayOfWeek = c.get(Calendar.DAY_OF_WEEK);
	    
	    int addDay = 0;
	    
	    if(dayOfWeek >= 5){
	    	addDay = 9-dayOfWeek;
	    }else{
	    	addDay = 2-dayOfWeek;
	    }
	    
	    c.add(Calendar.DATE, addDay);
	    returnDate[0] = c.getTime();
	    
	    c.add(Calendar.DATE, 4);
	    returnDate[1] = c.getTime();
	    
		return returnDate;
	}
	
	
	public static Date formatUsDateString(String strTime){
		SimpleDateFormat sdf = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z",Locale.US);
		sdf.setTimeZone(TimeZone.getTimeZone("GMT-9"));
		Date adate;
		try{
			adate = sdf.parse(strTime);
		}catch(Exception e){
			adate = null;
			throw new RuntimeException(e.getMessage()); 
		}
        return adate;
	}
	
	public static void main(String[] args) throws Exception{
		DateTools dt = new DateTools();
		//String strTime = "09 Jul 2012 08:16:54";
		String strTime = "Mon, 09 Jul 2012 08:16:54 +0800";
		//EEE, d MMM yyyy HH:mm:ss Z
		SimpleDateFormat sdf = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z",Locale.US);
		sdf.setTimeZone(TimeZone.getTimeZone("GMT-9"));

        Date adate = sdf.parse(strTime);

        System.out.println("lastBuildDate: " + adate);
        
        
        System.out.println("lastBuildDate: " + dt.getMonth(new Date()));

	}

}
